package cn.vw.hi.commen.tools;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Helper {
	private static final double EARTH_RADIUS = 6378137;

	// 乱码处理
	public static String decode(String text) {
		if (text == null) {
			return null;
		}
		try {
			return URLDecoder.decode(text, "utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return null;
	}

	// 乱码处理
	public static String encode(String text) {
		if (text == null) {
			return null;
		}
		try {
			return URLEncoder.encode(text, "utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return null;
	}

	// 乱码处理
	public static String convertToChinese(String str) {
		if (str == null) {
			return null;
		}
		String keyword = null;
		try {
			if (str != null) {
				keyword = new String(str.getBytes("ISO-8859-1"), "utf-8");
			}
		} catch (UnsupportedEncodingException e) {

			e.printStackTrace();
		}
		return keyword;
	}

	// 获得评论图标
	public static String criticismImage(int count) {
		String keyword = "";
		if (count == 0) {
			keyword = "<img  src='image/pingjia1.gif'/><img  src='image/pingjia1.gif'/><img  src='image/pingjia1.gif'/><img  src='image/pingjia1.gif'/><img  src='image/pingjia1.gif'/>";
		} else {
			for (int i = 0; i < 5; i++) {
				if ((i + 1) <= count) {
					keyword += "<img  src='image/pingjia2.gif'/>";
				} else {
					keyword += "<img  src='image/pingjia1.gif'/>";
				}
			}
		}

		return keyword;
	}

	// 去年html代码
	public static String deleteHtml(String content, int length, String str) {

		content = content.replaceAll("&nbsp;", "");
		content = content.replaceAll("\\<.*?>", "");
		if (length != 0) {
			if (content.length() > length) {
				content = content.substring(0, length) + str;
			}
		}
		return content;
	}

	// 获得日期
	public static String formatDate(String dateStr, int n) {

		SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
		Date date = null;
		try {
			date = myFormatter.parse(dateStr);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return myFormatter.format(new Date(date.getTime() + n * 24 * 60 * 60
				* 1000));
	}

	/** */
	/**
	 * 根据两点间经纬度坐标（double值），计算两点间距离，单位为米
	 * 
	 * @param lng1
	 * @param lat1
	 * @param lng2
	 * @param lat2
	 * @return
	 */
	public static double getDistance(double x1, double y1, double x2,
			double y2) {
		double a = y1 - y2;
		double b = x1 - x2;
		double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2)
				+ Math.cos(y1) * Math.cos( y2)
				* Math.pow(Math.sin(b / 2), 2)));
		s = s * EARTH_RADIUS;
		s = Math.round(s * 10000) / 10000;
		return s;
	}

	

	public static void main(String[] args) {
		double x1 = (121.441626548767);
		double y1 = (31.3312420379237);
		double x2 = (121.500849723816);
		double y2 = (31.3529406172867);
		double c1 = (121.44612188342285);
		double c2 = (31.330779645271377);
       //System.out.println(pointToLine(x1, y1, x2,y2,c1, c2));
       System.out.println(pointToLine(x1, y1, x2,y2,c1, c2));
	}

	public static double pointToLine(double x1, double y1, double x2, double y2, double c1,double c2)
	{
		double total=0D;
		 x1 = x1* Math.PI / 180.0;
		 y1 = y1* Math.PI / 180.0;
		 x2 = x2* Math.PI / 180.0;
		 y2 = y2* Math.PI / 180.0;
		 c1 = c1* Math.PI / 180.0;
		 c2 = c2* Math.PI / 180.0;
		double a, b, c;
        a = getDistance(x1, y1, x2, y2);// 线段的长度
        b = getDistance(x1, y1, c1, c2);// (x1,y1)到点的距离
        c = getDistance(x2, y2, c1, c2);// (x2,y2)到点的距离
        if (c * c >= a * a + b * b) {
        	total= b;
        }else if (b * b >= a * a + c * c) {
        	total= c;
        }else
        {
        	double d1=((x2-x1)*(c2-y1)/(y2-y1))+x1;
    		double d2=((y2-y1)*(c1-x1)/(x2-x1))+y1;
    		double dx=Math.abs(d1-c1);
    		double dy=Math.abs(d2-c2);
    		total=Math.round(Math.min(dx, dy)*EARTH_RADIUS*10000)/10000;
        }
      
		return total;
	}
}
